package com.fuershiye.woman.monitor.dao.admin;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fuershiye.woman.monitor.dao.HibernateDao;
import com.fuershiye.woman.monitor.model.Permission;

@Repository("adminPermissionDao")
public class PermissionDao {

    @Autowired
    private HibernateDao hibernateDao;
    
    public Permission findByName(String username){
        String sql = "select * from admin_permission where name = ?";
        SQLQuery query = hibernateDao.getSqlQuery(sql).addEntity(Permission.class);
        query.setString(0, username);
        return (Permission)query.uniqueResult();
    }

    public void save(Permission permission){
        hibernateDao.save(permission);
    }

    @SuppressWarnings("unchecked")
	public List<Permission> list() {
        String hql = "from Permission";
        Query query = hibernateDao.getQuery(hql);
        return query.list();
    }

    @SuppressWarnings("unchecked")
	public List<Permission> listByName(String name) {
        String hql = "from Permission where name like:name";
        Query query = hibernateDao.getQuery(hql);
        query.setString("name","%" + name + "%");
        return query.list();
    }

    public Permission get(Long id) {
        return hibernateDao.getById(Permission.class,id);
    }

    public void delete(Long id) {
        String hql = "delete Permission where id=:id";
        Query query = hibernateDao.getQuery(hql);
        query.setLong("id",id);
        query.executeUpdate();
    }
}
